package com.example.fengsam.aidong;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.fengsam.aidong.activity.BaseActivity;
import com.example.fengsam.aidong.activity.TabMessage;
import com.example.fengsam.aidong.fragment.ExerciseFragment;
import com.example.fengsam.aidong.fragment.MeFragment;
import com.example.fengsam.aidong.fragment.SocialFragment;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.BottomBarTab;
import com.roughike.bottombar.OnTabReselectListener;
import com.roughike.bottombar.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.bottomBar)
    BottomBar bottomBar;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    private BottomBarTab exerciseTab;
    private BottomBarTab socialTab;
    private BottomBarTab meTab;

    private ArrayList<Fragment> mFragmentList;
    private MyFragmentPagerAdapter adapter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initViewPager();

        exerciseTab=bottomBar.getTabAtPosition(0);
        socialTab=bottomBar.getTabAtPosition(1);
        meTab=bottomBar.getTabAtPosition(2);
        exerciseTab.setSelected(false);
        socialTab.setSelected(true);
        meTab.setSelected(false);

        bottomBar.setDefaultTabPosition(1);

        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
//                messageView.setText(TabMessage.get(tabId, false));
                mViewPager.setCurrentItem(bottomBar.findPositionForTabWithId(tabId));
            }
        });

        bottomBar.setOnTabReselectListener(new OnTabReselectListener() {
            @Override
            public void onTabReSelected(@IdRes int tabId) {
                Toast.makeText(getApplicationContext(), TabMessage.get(tabId, true), Toast.LENGTH_LONG).show();
            }
        });
    }

    public void initViewPager()
    {
        mFragmentList = new ArrayList<>();
        ExerciseFragment mExerciseFragment=new ExerciseFragment();
        MeFragment mMefragment=new MeFragment();
        SocialFragment mSocialFragment=new SocialFragment();

        mFragmentList.add(mSocialFragment);
        mFragmentList.add(mExerciseFragment);
        mFragmentList.add(mMefragment);

        adapter=new MyFragmentPagerAdapter(getSupportFragmentManager(),mFragmentList);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(1);
        mViewPager.addOnPageChangeListener(this);


    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state==2)
        {
            switch (mViewPager.getCurrentItem()){
                case 0:
                    bottomBar.selectTabAtPosition(0,true);
                    break;
                case 1:
                    bottomBar.selectTabAtPosition(1,true);
                    break;
                case 2:
                    bottomBar.selectTabAtPosition(2,true);
                    break;
            }
        }
    }

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragmentList;
        private FragmentManager fm;

        public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fm = fm;
            this.fragmentList = fragmentList;
            Log.e("adapter", "fragment");
        }


        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public Object instantiateItem(ViewGroup vg, int position) {
            Fragment fragment = (Fragment) super.instantiateItem(vg,
                    position);
            fm.beginTransaction().show(fragment).commit();
            return fragment;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            Fragment fragment = fragmentList.get(position);
            fm.beginTransaction().hide(fragment).commit();
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

    }
}
